//
//  Create 7z Archive (Folder Only).m
//  Create 7z Archive (Folder Only)
//
//  http://automator.proj8x.org
//
//  Created by Tiago Nodari on 10/6/09.
//  Copyright (c) 2009 Tiago Nodari
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
// 
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
//



#import "Create 7z Archive (Folder Only).h"


@implementation Create_7z_Archive__Folder_Only_

- (id)runWithInput:(id)input fromAction:(AMAction *)anAction error:(NSDictionary **)errorInfo
{
	
	NSMutableArray* output = [[NSMutableArray alloc] init];
	NSTask *p7z ;
	
	BOOL isInputTheOuput = [[[self parameters] valueForKey:@"isOutputSameAsInput"] boolValue];
	
	if ([input count] > 0) 
	{
		@try
		{
			NSString* strArchiveDestinationDir = [[self parameters] valueForKey:@"archiveDestinationDirectory"];
			NSString* strPathTo7za = [[self parameters] valueForKey:@"pathTo7za"];
			
			
			if ([strPathTo7za length] == 0) 
			{
				[strPathTo7za initWithString:@"/Applications/7za"];
			}
			//TODO Need to add error handling if cannot locate 7za
			
			BOOL destinationDirProvided = FALSE;
			if ([strArchiveDestinationDir length] > 0) 
			{
				destinationDirProvided = TRUE;
			}
			
			
			
			for( NSString* inputPath in input ) 
			{
				//TODO need to check if the input is a directory
				p7z = [[NSTask alloc] init];
				NSMutableArray* args = [[NSMutableArray alloc] init];
				
				[p7z setLaunchPath: strPathTo7za];
				[p7z setStandardInput:[NSPipe pipe]];
				
				NSPipe *errorPipe;
				errorPipe = [NSPipe pipe];
				[p7z setStandardError: errorPipe];
				
				[args addObject: @"a"];
				
				//TODO - Clean up after learn memory management.
				if (destinationDirProvided) 
				{
					NSString* archiveName = [[NSString alloc] initWithFormat:@"%@/%@.7z", strArchiveDestinationDir, [inputPath lastPathComponent]];
					[args addObject: [archiveName stringByStandardizingPath]];
					[output addObject: [archiveName stringByStandardizingPath]];
				}
				else 
				{
					NSString* archiveName = [[NSString alloc] initWithFormat:@"%@.7z",  inputPath];
					[args addObject: [archiveName stringByStandardizingPath]];
					[output addObject: [archiveName stringByStandardizingPath]];
				}

				
				[args addObject: [NSString stringWithFormat: inputPath]];
				
				[p7z setArguments:args];
				
				[p7z launch];
				[p7z waitUntilExit];
				
				int exitCode = [p7z terminationStatus];
				if (exitCode != 0) 
				{
					// TODO - Need to throw a real error!!!!
					NSFileHandle *errorFile;
					errorFile = [errorPipe fileHandleForReading];
					NSData *errorData;
					errorData = [errorFile readDataToEndOfFile];
					NSString *errorSTR;
					errorSTR = [[NSString alloc] initWithData: errorData encoding: NSUTF8StringEncoding];
					
					NSAlert *alert = [[NSAlert alloc] init];
					[alert addButtonWithTitle:@"OK"];
					[alert setMessageText:@"Error"];
					
					[alert setInformativeText:[NSString stringWithFormat:@"ExitCode: %d Pipe Result:%@  ",exitCode,errorSTR]];
					[alert runModal];	
					[alert release]; 
					[errorData release];
					[errorFile release];
					[errorSTR release];
					
				}
				[args release];
				[p7z release];
			}
			
			
		} 
		@catch (NSException *exception) 
		{
			@throw exception;
		} 
		@finally 
		{
			[p7z release];
		}			
		
	}
	
	
	
	
	if (isInputTheOuput) 
	{
		return input;
	}
	else 
	{
		return output;
	}

		


	
	
}

@end
